package com.k.sshm.boot.web.servlet.container.control;

import com.k.sshm.boot.web.servlet.container.model.ModelMV;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;


@Controller
@RequestMapping("/param")
public class ParamControl {
    @ResponseBody
    @PostMapping(path = "/post", consumes = "application/json", produces = "application/json", headers = "myHeader=myValue")
    public String post(@RequestBody ModelMV pet, Reader reader, ServletRequest req, ServletResponse resp) {
        return "ok";
    }

    @GetMapping("/get/{id}")
    public ModelMV get(@PathVariable Long id, HttpSession session, HttpMethod method, InputStream input, OutputStream output, Writer writer) {
        return new ModelMV();
    }

    @PostMapping("/param")
    @ResponseBody
    public ModelMV param(@RequestParam String id, @RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive") long keepAlive, @CookieValue("JSESSIONID") String cookie) {
        return new ModelMV();
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            return "redirect:uploadSuccess";
        }
        return "redirect:uploadFailure";
    }

    @PostMapping("/resp")
    @ResponseBody
    public ModelMV param() {
        ModelMV modelMV = new ModelMV();
        modelMV.setId("123");
        return modelMV;
    }
}
